package ms07.ticketPro.activities;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;

import ms07.ticketPro.DebugLog;
import ms07.ticketPro.MyApplication;
import ms07.ticketPro.R;
import ms07.ticketPro.Ticket;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class ActivityTickets extends ListActivity{
	protected static ActivityTickets instance;
	public static ActivityTickets getInstance(){
		if(instance==null){
			new ActivityTickets();
		}
		return instance;
	}
	
	public ActivityTickets(){
		super();
		instance = this;
	}
	
	ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
	private static final int REQUEST_TICKET_ID = 1;
	private static final int SETTINGS_ID = 2;
	private static final int ABOUT_ID = 3;
	private static final int DELETE_ID = 4;
	private static final int SHARE_ID = 5;
	private static final int OPEN_ID = 6;
	
	private SimpleAdapter notes;
	protected MyApplication application;
	
	@Override  
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);  
		this.setContentView(R.layout.tickets);  
		this.registerForContextMenu(this.getListView()); //add context menu
		
		this.notes = new SimpleAdapter(
				this,
				this.list,
				R.layout.ticket_list_entry,
				new String[]{ "title","text" },
				new int[]{ R.id.ticket_list_entry_title, R.id.ticket_list_entry_text }  );
		
		this.setListAdapter(this.notes);
		
		this.application = (MyApplication)this.getApplication();

		List<Ticket> tickets = this.application.getDatabase().selectAllTickets();
		
		for(Ticket ticket: tickets){
			this.addTicket(ticket);
		}
	}
	
	public boolean onCreateOptionsMenu(Menu menu) {
		boolean result = super.onCreateOptionsMenu(menu);

		menu.add(0, REQUEST_TICKET_ID, Menu.NONE, R.string.request_ticket ).setIcon(R.drawable.ic_menu_add);
		menu.add(0, SETTINGS_ID, Menu.NONE, R.string.settings ).setIcon(R.drawable.ic_menu_preferences);
		menu.add(0, ABOUT_ID, Menu.NONE, R.string.about ).setIcon(R.drawable.ic_menu_info_details);
		
		return result;
	}
	
	public boolean onOptionsItemSelected(MenuItem item){
		Intent myIntent;
		
		switch(item.getItemId()){
			case REQUEST_TICKET_ID:
				//open request view for new ticket

				//TODO SHOW TICKET DETAIS!!!
				myIntent = new Intent(this.getBaseContext(), ActivityTicketRequest.class);
				this.startActivityForResult(myIntent,0);
		        
				break;
			case SETTINGS_ID: 
				//open settings dialog
				
				myIntent = new Intent(this.getBaseContext(), ActivityPreferences.class);
				
				this.startActivity(myIntent);
				break;
			case ABOUT_ID: 
				//open settings dialog
				break;
		}
		return super.onOptionsItemSelected(item);
	}
	
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo){
		super.onCreateContextMenu(menu, v, menuInfo);
		
		menu.add(0, OPEN_ID, 0, R.string.open_ticket);
		menu.add(0, SHARE_ID, 0, R.string.share_ticket).setEnabled(false);
		menu.add(0, DELETE_ID, 0, R.string.delete_ticket);
	}
	
	@SuppressWarnings("unchecked")
	public boolean onContextItemSelected(MenuItem item) {
		AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();

		if(!(this.getListAdapter().getItem(info.position) instanceof HashMap<?,?>)){
			//show error
			DebugLog.getInstance().error("klicked OPEN on ticket"+info.position);
			
			return super.onContextItemSelected(item);
		}
		
		
		HashMap<String,String> tmp = (HashMap<String,String>)this.getListAdapter().getItem(info.position);//this.list.get(info.position);
		
		DebugLog.getInstance().log("INTERNAL ID IS="+tmp.get("internal_id"));
		
		Ticket ticket = Ticket.getTicketInstance(new Long(tmp.get("internal_id")));
		
		switch (item.getItemId()) {
			case OPEN_ID:
				//info.position;
				DebugLog.getInstance().log("klicked OPEN on ticket"+info.position);
				this.openTicket(ticket);
				return true;
			case SHARE_ID:
				DebugLog.getInstance().log("klicked SHARE on ticket"+info.position);
				//TODO IMPLEMENT SHARING!!
				return true;
			case DELETE_ID:
				DebugLog.getInstance().log("klicked DELETE on ticket"+info.position);

				ticket.delete();
				this.list.remove(tmp);
				this.notes.notifyDataSetChanged();
				
				return true;
			default:
				return super.onContextItemSelected(item);
		}
	}
	
	protected void onListItemClick(ListView l, View v, int position, long id) {

		if(!(this.getListAdapter().getItem(position) instanceof HashMap<?,?>)){
			//show error
			DebugLog.getInstance().error("klicked OPEN on ticket"+position);
			
			super.onListItemClick(l, v, position, id);
			return;
		}
		
		HashMap<String,String> tmp = (HashMap<String,String>)this.getListAdapter().getItem(position);//this.list.get(info.position);
		
		DebugLog.getInstance().log("INTERNAL ID IS="+tmp.get("internal_id"));
		
		Ticket ticket = Ticket.getTicketInstance(new Long(tmp.get("internal_id")));
		
		this.openTicket(ticket);
		super.onListItemClick(l, v, position, id);
	}
	
	public void openTicket(Ticket ticket){
		//TODO SHOW TICKET DETAIS!!!
		Intent myIntent = new Intent(this, ActivityTicketDetails.class);
		myIntent.putExtra("internal_id", ticket.getInternalId());
		myIntent.putExtra("ticket", ticket);
        startActivityForResult(myIntent, 0);
	}
	
	public void addTicket(Ticket ticket){
		DebugLog.getInstance().log("add ticket to Tickets list!");

		DateFormat date_format = DateFormat.getDateTimeInstance();
		
		HashMap<String,String> item = new HashMap<String,String>();
		item.put("title", "#"+ticket.getInternalId()+" "+ticket.getOrganisation()+": "+ticket.getCity_from()+" nach "+ticket.getCity_to() );
		item.put("text","Gültig am "+date_format.format(ticket.getValid_from())+" für "+ticket.getAdults()+"E");
		item.put("internal_id", Long.toString(ticket.getInternalId()));
		this.list.add( item );
		
		this.notes.notifyDataSetChanged();
//		TableRow tr;
//		TextView t;
//		
//		/* Find Tablelayout defined in main.xml */
//		TableLayout tl = (TableLayout)findViewById(R.id.myTableLayout);
//			 /* Create a new row to be added. */
//			tr = new TableRow(this);
//				t = new TextView(this);
//				t.setText(ticket.getCity_from()+" -> "+ticket.getCity_to());
//				tr.addView(t);
//			tl.addView(tr);
//				
//
//			tr = new TableRow(this);
//				t = new TextView(this);
//				t.setText("Adults: "+ticket.getAdults()+", Children: "+ticket.getChildren());
//				tr.addView(t);
//				
//				t = new TextView(this);
//				t.setText("Preis: "+ticket.getPrice()+" EUR");
//				tr.addView(t);
//				
//				
//			tl.addView(tr);
	}
}


